/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package gword;

import gword.generateur.WordMLGenerator;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JOptionPane;

import kameleon.document.Document;
import kameleon.exception.FileReadingException;
import kameleon.exception.FileWritingException;
import kameleon.exception.KameleonException;
import kameleon.plugin.SupportedOptions;
import kameleon.util.FileConstants;
import kameleon.util.IOFile;

/**
 * 
 * 
 * @author		Brabant Quentin, Fromentin Xavier, Schnell Michaël
 * @version		1.1
 */
public class Main extends kameleon.plugin.Generator implements FileConstants {
	
	/**
	 * Identifier used by this plug-in.
	 */
	private static final String GENERATOR_ID = "WordML2003Generator" ; //$NON-NLS-1$
	
	/**
	 * Location of the picture inside the java archive of this plug-in.
	 */
	private static final String PICTURE_LOCATION = "/gword/resources/%s" ; //$NON-NLS-1$
	
	/**
	 * Array containing the names of the pictures used by this plug-in.
	 * 
	 * @see		#copyPicture(String)
	 */
	private static final String[] PICTURE_NAMES = new String[]{
		String.format(FORMAT_ICON_FILE_NAME, GENERATOR_ID), 
		String.format(FORMAT_GRAY_ICON_FILE_NAME, GENERATOR_ID), 
		String.format(FORMAT_MINI_FILE_NAME, GENERATOR_ID)
	} ;
	
	protected boolean debugMode ;
	
	//TODO Review
	public Main() {
		this(true) ;
	}
	
	/**
	 * Sole constructor.
	 */
	public Main(boolean debugMode) {
		super() ;
		this.debugMode = debugMode ;
	}// Main()

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void copyPicture(String targetFolder) throws KameleonException {
		for(String pictureName : PICTURE_NAMES) {
			// Create the streams for the copying
			InputStream src ;
			OutputStream dest ;
			String picLocation = String.format(PICTURE_LOCATION, pictureName) ;
			src = this.getClass().getResourceAsStream(picLocation) ;
			if (src == null) {
				throw new FileReadingException(new File(picLocation)) ;
			}// if
			src = new BufferedInputStream(src) ;
			
			File targetFile = new File(String.format(PATH_EXTENSION, targetFolder, pictureName)) ;
			try {
				dest = new BufferedOutputStream(new FileOutputStream(targetFile)) ;
			} catch (FileNotFoundException e) {
				throw new FileWritingException(targetFile) ;
			}// try

			// Do the actual copying
			IOFile.copyFile(src, dest) ;

			// Close the streams
			try {
				dest.close() ;
			} catch (IOException oie) {
				throw new FileWritingException(targetFile) ;
			}// try
			try {
				src.close() ;
			} catch (IOException e) {
				//TODO Review
				/* No need to handle. */
			}// try
		}// for
	}// copyPicture(String)

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void generate(Document base, String filePath,
			SupportedOptions options) throws KameleonException {
//		gword.generateur.Generator generator = new gword.generateur.Generator(new File(filePath)) ;
//		generator.generateDocument(base, options) ;
		WordMLGenerator generator = new WordMLGenerator(this.debugMode) ;
		generator.generateDocument(base, filePath, options) ;
	}// generate(Document, String, SupportedOptions)

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void generate(Document base, String filePath) throws KameleonException {
		//TODO Temporary - replace with default options
		this.generate(base, filePath, null) ;
	}// generate(Document, String)

}// class Main